# -*- coding: utf-8 -*-
from threading import Thread

import uvicorn

from fastapi import FastAPI

app = FastAPI()


class FastAPIThread(Thread):
    def __init__(self):
        super().__init__()
        self.should_stop = False

    def run(self):
        self.serve_fastapi()

    def serve_fastapi(self):
        # 启动FastAPI服务器
        uvicorn.run(
            app,
            host="127.0.0.1",
            port=9999,
            log_level="info",
            reload=False
        )

    def stop(self):
        self.should_stop = True

    @app.get("/")
    async def hello():
        return "Hello World!"


if __name__ == '__main__':
    try:
        fastapi_thread = FastAPIThread()
        fastapi_thread.start()
        print("run  ........")
    except Exception as e:
        print(f"解析数据错误: {e}")
